package org.jeecg.modules.quartz.job;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;

import org.jeecg.common.nc.enums.WSEnum;
import org.jeecg.common.nc.service.NCWebService;
import org.jeecg.common.nc.vo.WSVO;
import org.jeecg.modules.nc.entity.NcMarbasclass;
import org.jeecg.modules.nc.service.INcMarbasclassService;
import org.jeecg.modules.quartz.entity.TbJobLog;
import org.jeecg.modules.quartz.service.IQuartzJobService;
import org.jeecg.modules.quartz.service.ITbJobLogService;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import java.util.Date;
import java.util.Map;

/**
 * NC物料分类同步
 */
public class NCMarbasclassSyncJob implements Job {

    private static final Logger logger = LoggerFactory.getLogger(NCMarbasclassSyncJob.class);

    @Autowired
    private INcMarbasclassService ncMarbasclassService;

    @Autowired
    IQuartzJobService jobService;

    @Autowired
    private ITbJobLogService tbJobLogService;


    @Value("${qixin.ncserver.uri}")
    private String NC_SERVER_URI;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {

        String lastTime = jobService.queryLastTime("ncmarbasclasssyncjob");
        String currentTime = DateUtil.now();
        Boolean first = false;
        if (StrUtil.isBlank(lastTime)){
            lastTime = "2008-01-01 00:00:00";
            first = true; // 表示第一次同步
        }

        // 获取商品分类
        try {
            WSVO vo = NCWebService.getFromWS(NC_SERVER_URI, WSEnum.SHANGPINFENLEI,lastTime);
            for (Map<String, Object> map : vo.getContent()) {
                NcMarbasclass ncMarbasclass = new NcMarbasclass();
                ncMarbasclass.setSid((String) map.get("pk_marbasclass"));
                ncMarbasclass.setCode((String) map.get("code"));
                ncMarbasclass.setPkParent((String) map.get("pk_parent")); // 上级分类
                ncMarbasclass.setName((String) map.get("name"));
                ncMarbasclass.setUpdateTime(new Date());
                ncMarbasclassService.saveOrUpdate(ncMarbasclass);

            }
            TbJobLog tbJobLog = new TbJobLog();
            tbJobLog.setName("NC物料分类同步");
            tbJobLog.setContent("本次新增或更新记录" + vo.getContent().size() + "条");
            tbJobLog.setNum(vo.getContent().size());
            tbJobLogService.save(tbJobLog);
            if(first){
                // 新增一条记录
                jobService.insetLastTime("ncmarbasclasssyncjob", currentTime);
            }else{
                //更新最后同步时间
                jobService.updateLastTime("ncmarbasclasssyncjob", currentTime);
            }
            logger.info("NC物料分类同步成功更新：{}条", vo.getContent().size());
        } catch (Exception e) {
            logger.info("NC物料分类同步出现异常：{}", e.getMessage());
            e.printStackTrace();
            TbJobLog tbJobLog = new TbJobLog();
            tbJobLog.setName("NC物料分类同步");
            tbJobLog.setContent("同步出现异常：" + e.getMessage());
            tbJobLog.setNum(0);
            tbJobLogService.save(tbJobLog);
        }

    }
}
